<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        div{
            width: 150px;
            height: 150px;
            border: 1px solid #000;
            margin-top: 300px;
            margin-left: 100px;
            position: relative;
            background: url(../images/s4.png);
            background-size: 100% 100%;
        }
        /* img{
            width: 100%;
            height: 100%;
        } */
        p{
            width: 80px;
            height: 80px;
            background: rgba(193, 241, 48, 0.5);
            position: absolute;
            display: none;
        }
        nav{
            width: 400px;
            height: 400px;
            border: 2px solid #000;
            background: red;
            position: fixed;
            top: 150px;
            left: 250px;
            display: none;
            overflow: hidden;
        }
        img{
            position: absolute;
            width: 1150px;
            height: 1150px;
        }
    </style>
</head>
<body>
    <div>
        <p></p>
        <!-- <img src="../images/4-4.png" alt=""> -->
    </div>
    <nav>
        <img src="../images/4-4.png" alt="">
    </nav>
</body>
</html>
<script>
    const div = document.querySelector('div')
    const p = document.querySelector('p')
    const nav = document.querySelector('nav')

    //鼠标划入事件
    div.addEventListener('mouseover',() => {
        p.style.display = 'block'
        nav.style.display = 'block'
    })
    //鼠标划出事件
    div.addEventListener('mouseout',() => {
        p.style.display = 'none'
        nav.style.display = 'none'
    })

    // let offsetX = 0;
    // let offsetY = 0;
    // div.addEventListener('mousemove',(e) => {
    //     const X = 0;
    //     const Y = 0;
    //     offsetX = e.offsetX - e.clientX;
    
    //     offsetY = e.offsetY - e.clientY;
    //     p.style.top = offsetX + 'px'

    // })
    
    //鼠标移动事件
    div.addEventListener('mousemove',fn => {
        //小块移动

            let offsetX = fn.pageX - div.offsetLeft
            let offsetY = fn.pageY - div.offsetTop
            // console.log(offsetX,offsetY);

            // 让鼠标位于 小块 中心位置
            let mackX = offsetX - p.offsetWidth / 2
            let mackY = offsetY - p.offsetHeight / 2

            // 获取小块 在大块内 最大 移动 距离  // 正方形 只计算宽，就等于高
            let mackMax = div.offsetWidth - p.offsetWidth
            // console.log(div.offsetWidth - p.offsetWidth);

            // 小块碰到边框 之后 不再移动
            if(mackY <= 0){
                mackY = 0
            }else if(mackY >= mackMax){
                mackY = mackMax
            }
            if(mackX <= 0){
                mackX = 0
            }else if(mackX >= mackMax){
                mackX = mackMax
            }

            p.style.left = mackX + 'px'
            p.style.top = mackY + 'px'

        

        //大块移动

            const img = document.querySelector('img')
            
            //大块可移动 最大 距离
            let max = img.offsetWidth - nav.offsetWidth
    
            // console.log(img.offsetWidth - nav.offsetWidth);
            
            //计算出 小块每移动多少，大块移动多少
            let imgX = (mackX * max) / mackMax 
            let imgY = (mackY * max) / mackMax

            // console.log(imgX,imgY);
    
            img.style.top = -imgY + 'px'
            img.style.left = -imgX + 'px'

        
     })






</script>